// LevelCtl.cpp: Implementierung der Klasse CLevelCtl.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LevelCtl.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

CLevelCtl::CLevelCtl()
	{
	BezugRahmen = NULL;
	MaxVal = 100;
	MinPixel = 3;
	LastCol = 0;
	Col = RGB(0,0,255);
	Val = 0;
	LastH = 0;
	H = 1;
	On = true;
	}

	
CLevelCtl::CLevelCtl(CStatic &aRahmen)
	{
	BezugRahmen = &aRahmen;
	MaxVal = 100;
	MinPixel = 3;
	LastCol = 0;
	Col = RGB(0,0,255);
	Val = 0;
	LastH = 0;
	H = 1;
	On = true;
	}


CLevelCtl::~CLevelCtl()
	{
	}


void CLevelCtl::SetFrame(CStatic &aRahmen)
	{
	BezugRahmen = &aRahmen;
	}


void CLevelCtl::InitPosition()
	{
	ASSERT(BezugRahmen != NULL);
	BezugRahmen->GetWindowRect(&Rect);
	BezugRahmen->GetParent()->ScreenToClient(&Rect);
	Rect.DeflateRect(2,2,2,2);
	SetVal(0);
	BezugRahmen->GetParent()->InvalidateRect(Rect);
	}


void CLevelCtl::SetMaxVal(int NewMax)
	{
	ASSERT(BezugRahmen != NULL);
	MaxVal = NewMax;
	SetVal(Val);
	}


void CLevelCtl::SetVal(int aVal)
	{
	ASSERT(BezugRahmen != NULL);
	Val = aVal;
	if (Val < 0) 
		Val = 0;
	else if (Val > MaxVal)
		Val = MaxVal;
	H = long(Rect.Height() - MinPixel) * (MaxVal - Val) / MaxVal;
	}


void CLevelCtl::SetColor(COLORREF c)
	{
	Col = c;
	}


void CLevelCtl::SetOn(bool aOn)
	{
	ASSERT(BezugRahmen != NULL);
	if (On != aOn)
		{
		On = aOn;
		BezugRahmen->GetParent()->InvalidateRect(Rect);
		}
	}


void CLevelCtl::Refresh()
	{
	ASSERT(BezugRahmen != NULL);
	if (On && (H != LastH || Col != LastCol))
		BezugRahmen->GetParent()->InvalidateRect(Rect);
	}


void CLevelCtl::Paint(CDC &dc)
	{
	ASSERT(BezugRahmen != NULL);
	if (On)
		{
		CRect hr = Rect;
		hr.DeflateRect(0, H, 0, 0);
		dc.FillSolidRect(hr, Col);
		LastH = H;
		LastCol = Col;
		}
	else
		LastH = Rect.Height();
	}


